import json
import os

# 读取military.json文件
input_file = '/Users/mini/work/llm/Hyper-RAG/wq/data/military.json'
output_dir = '/Users/mini/work/llm/Hyper-RAG/wq/source'

# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)

# 读取文件内容
with open(input_file, 'r', encoding='utf-8') as f:
    content = f.read()

# 由于JSON对象之间没有逗号分隔，需要手动分割
json_objects = []
current_object = ''
braces_count = 0

for char in content:
    current_object += char
    if char == '{':
        braces_count += 1
    elif char == '}':
        braces_count -= 1
        if braces_count == 0:
            # 去除前后空白字符
            current_object = current_object.strip()
            if current_object:
                try:
                    # 解析JSON对象
                    obj = json.loads(current_object)
                    json_objects.append(obj)
                except json.JSONDecodeError:
                    print(f"Failed to parse JSON: {current_object[:50]}...")
            current_object = ''

# 处理前3个对象（根据用户要求）
for i, obj in enumerate(json_objects):
    if '_id' in obj and '$oid' in obj['_id']:
        oid = obj['_id']['$oid']
        output_file = os.path.join(output_dir, f'{oid}.json')
        
        # 写入单个JSON文件
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump(obj, f, ensure_ascii=False, indent=2)
            
        print(f'Created file: {output_file}')

print(f'Processed {len(json_objects)} JSON objects.')